﻿using AutoMapper;
using HealthcareSystem.API.Write.APPLication.Commad.KnowledgeCommand;
using HealthcareSystem.Domain.Knowleges;
using MediatR;
using WeekExam.PassPort.ErrorCode;
using WeekExam.PassPort.Infrastructure.Interface;

namespace HealthcareSystem.API.Write.APPLication.CommadHandel.KnowledgeCommand
{
    /// <summary>
    /// 我的知识添加
    /// </summary>
    public class AddknowledgeCommandIHandler : IRequestHandler<AddknowledgeCommand, ApiResult<int>>
    {
        IBaseRepository<Knowledge> knowledgeHandler;
        private readonly IMapper mapper;

        public AddknowledgeCommandIHandler(IBaseRepository<Knowledge> knowledgeHandler,IMapper mapper)
        {
            this.knowledgeHandler = knowledgeHandler;
            this.mapper = mapper;
        }
        /// <summary>
        /// 我的知识-新增处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<int>> Handle(AddknowledgeCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ResultCode.Success;
            result.Message = ResultCode.添加成功.ToString();

            var knowledge = mapper.Map<Knowledge>(request);
            await knowledgeHandler.AddAsync(knowledge);


            return await Task.FromResult(result);

        }
    }
}
